package com.aimee.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.aimee.entity.Page;

public class PermissionFilter implements Filter {

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		String uri = req.getRequestURI();
		System.out.println("进入过滤器--" + uri);
		// 怎么控制，你没有权限的页面不能看？？
		HttpSession session = req.getSession();
		List<Page> pagelist = (List) session.getAttribute("pageList");
		boolean find = false;
		for (Page page : pagelist) {
			System.out.println(page.getPage_name());
			if (uri.endsWith(page.getPage_name())) {
				find = true;
				break;
			}
		}

		if (find) {
			chain.doFilter(request, response);// 放行
		} else {
			// 跳转界面
			System.out.println("权限过滤器--无权访问");
		}

	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
